﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="PetitionerMatters", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(PetitionerMatters))]
    public class PetitionerMattersSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IPetitionerMatters clazz;
        
        public PetitionerMattersSerializer(IPetitionerMatters clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("PetitionerData");
            if (clazz.PetitionerData!=null) {
                global::Xtee.Core.SerializationMetadata metaPetitionerData = this.GetMetaData(clazz.PetitionerData);
                Writer.WriteTypeAttribute(metaPetitionerData.TypeQualifiedName.Namespace, metaPetitionerData.TypeQualifiedName.Name);
                metaPetitionerData.NewSerializer(clazz.PetitionerData).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : PetitionerData");
            }
            Writer.WriteEndElement();
            for (int x = 0; ((clazz.RepresentativeData != null) 
                        && (x < clazz.RepresentativeData.Count)); x = (x + 1)) {
                if (clazz.RepresentativeData[x]!=null) {
                    Writer.WriteStartElement("RepresentativeData");
                    global::Xtee.Core.SerializationMetadata metaRepresentativeData = this.GetMetaData(clazz.RepresentativeData[x]);
                    Writer.WriteTypeAttribute(metaRepresentativeData.TypeQualifiedName.Namespace, metaRepresentativeData.TypeQualifiedName.Name);
                    metaRepresentativeData.NewSerializer(clazz.RepresentativeData[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            if (((clazz.RepresentativeMatter == null) 
                        || (clazz.RepresentativeMatter.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "RepresentativeMatter");
            }
            for (int x = 0; ((clazz.RepresentativeMatter != null) 
                        && (x < clazz.RepresentativeMatter.Count)); x = (x + 1)) {
                Writer.WriteStartElement("RepresentativeMatter");
                if (clazz.RepresentativeMatter[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaRepresentativeMatter = this.GetMetaData(clazz.RepresentativeMatter[x]);
                    Writer.WriteTypeAttribute(metaRepresentativeMatter.TypeQualifiedName.Namespace, metaRepresentativeMatter.TypeQualifiedName.Name);
                    metaRepresentativeMatter.NewSerializer(clazz.RepresentativeMatter[x]).WriteXml(Writer);
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : RepresentativeMatter");
                }
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="PetitionerData") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>PetitionerMatters>PetitionerData");
                }
                global::Xtee.Core.SerializationMetadata metaPetitionerData = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IPetitionerData>(Reader, "PetitionerData", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.PetitionerData = metaPetitionerData.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IPetitionerData>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("PetitionerData");
                    metaPetitionerData.NewSerializer(clazz.PetitionerData).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>PetitionerMatters>PetitionerData", Reader.LocalName);
            }
            if (Reader.LocalName=="RepresentativeData") {
                clazz.RepresentativeData = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativeData>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "RepresentativeData")); ) {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>PetitionerMatters>RepresentativeData");
                    }
                    global::Xtee.Core.SerializationMetadata metaRepresentativeData = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativeData>(Reader, "RepresentativeData", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativeData item = metaRepresentativeData.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativeData>();
                    clazz.RepresentativeData.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("RepresentativeData");
                        metaRepresentativeData.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
            }
            clazz.RepresentativeMatter = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativeMatter>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "RepresentativeMatter")); ) {
                if (Reader.LocalName=="RepresentativeMatter") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>PetitionerMatters>RepresentativeMatter");
                    }
                    global::Xtee.Core.SerializationMetadata metaRepresentativeMatter = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativeMatter>(Reader, "RepresentativeMatter", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativeMatter item = metaRepresentativeMatter.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativeMatter>();
                    clazz.RepresentativeMatter.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("RepresentativeMatter");
                        metaRepresentativeMatter.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>PetitionerMatters>RepresentativeMatter", Reader.LocalName);
                }
            }
            if ((clazz.RepresentativeMatter.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "RepresentativeMatter");
            }
        }
    }
}
